package cn.fl.institution.service.impl;

import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.capital.transform.CapFundVOTransform;
import cn.fl.capital.vo.CapFundVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.institution.api.IPrjInsRedmInfoApiService;
import cn.fl.institution.api.IPrjInsRedmInterfaceInfoApiService;
import cn.fl.institution.condition.PrjInsRedmInfoQC;
import cn.fl.institution.condition.PrjInsRedmInterfaceInfoQC;
import cn.fl.institution.constant.PrjInsRedmInterfaceInfoConstant;
import cn.fl.institution.dto.PrjInsRedmInfoDTO;
import cn.fl.institution.dto.PrjInsRedmInterfaceInfoDTO;
import cn.fl.institution.service.IPrjInsRedmInfoService;
import cn.fl.institution.transform.PrjInsRedmInfoVOTransform;
import cn.fl.institution.vo.PrjDtlInsRedmInfoVO;
import cn.fl.institution.vo.PrjInsRedmInfoVO;
import com.alibaba.fastjson.JSON;
import fl.flow.facade.flow.dto.FlowStartedDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @Description: 资管项目赎回表 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class PrjInsRedmInfoServiceImpl implements IPrjInsRedmInfoService {


    /**
     * 自定义 API 自行注入需要的API服务
     */
    @Autowired
    private IPrjInsRedmInfoApiService prjInsRedmInfoApiService;

    @Autowired
    private IPrjInsRedmInterfaceInfoApiService prjInsRedmInterfaceInfoApiService;


    @Override
    public PrjInsRedmInfoVO selectById(Long id) throws BaseException {
        if (StringUtils.isEmpty(id)) {
            BaseLogUtils.newLogger("主键参数不能为空!").setKeys("selectById",CurrentThreadContext.getCurrentUserName()).info();
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "获取对象主键不能为空!");
        }
        PrjInsRedmInfoDTO dto = prjInsRedmInfoApiService.selectById(id);
        return PrjInsRedmInfoVOTransform.toVO(dto);
    }

    @Override
    public Long add(PrjInsRedmInfoVO entity) throws BaseException {
        if (null == entity) {
            BaseLogUtils.newLogger("新增对象不存在!").setKeys("add",CurrentThreadContext.getCurrentUserName()).info();
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "新增对象不存在!");
        }
        return this.prjInsRedmInfoApiService.add(PrjInsRedmInfoVOTransform.toDTO(entity));
    }

    @Override
    public boolean removeById(Long id) throws BaseException {
        if (StringUtils.isEmpty(id)) {
            BaseLogUtils.newLogger("删除对象主键不能为空!").setKeys("removeById",CurrentThreadContext.getCurrentUserName()).info();
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "删除对象主键不能为空!");
        }
        return this.prjInsRedmInfoApiService.removePrjInsRedmInfoDTOById(id);
    }

    @Override
    public boolean removeByIds(List<Long> ids) throws BaseException {
        return false;
    }


    @Override
    public boolean modify(PrjInsRedmInfoVO entity) throws BaseException {
        if (null == entity) {
            BaseLogUtils.newLogger("修改对象不能为空!").setKeys("modify",CurrentThreadContext.getCurrentUserName()).info();
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "修改对象不能为空!");
        }
        return this.prjInsRedmInfoApiService.modify(PrjInsRedmInfoVOTransform.toDTO(entity));
    }

    @Override
    public Pager searchListPage(Pager page) throws BaseException {
        Pager paper = this.prjInsRedmInfoApiService.searchPrjInsRedmInfoListPage(page);
        List<PrjInsRedmInfoDTO> dtoList = (List<PrjInsRedmInfoDTO>) paper.getResultList();
        if (dtoList != null && !dtoList.isEmpty()) {
            List<PrjInsRedmInfoVO> voList = PrjInsRedmInfoVOTransform.toVOList(dtoList);
            paper.setResultList(voList);
        }
        return paper;
    }

    @Override
    public Pager searchAssetsListPage(Pager page) throws BaseException {
        Pager paper = this.prjInsRedmInfoApiService.searchAssetsListPage(page);
        List<PrjInsRedmInfoDTO> dtoList = (List<PrjInsRedmInfoDTO>) paper.getResultList();
        if (dtoList != null && !dtoList.isEmpty()) {
            List<PrjInsRedmInfoVO> voList = PrjInsRedmInfoVOTransform.toVOList(dtoList);
            paper.setResultList(voList);
        }
        return paper;
    }

    @Override
    public Pager searchAssetsRedmListPage(Pager page) throws BaseException {
        Pager paper = this.prjInsRedmInfoApiService.searchAssetsRedmListPage(page);
        List<PrjInsRedmInfoDTO> dtoList = (List<PrjInsRedmInfoDTO>) paper.getResultList();
        if (dtoList != null && !dtoList.isEmpty()) {
            List<PrjInsRedmInfoVO> voList = PrjInsRedmInfoVOTransform.toVOList(dtoList);
            paper.setResultList(voList);
        }
        return paper;
    }

    /**
     * 查询资管资产赎回申请信息
     *
     * @param qc
     * @return
     */
    @Override
    public PrjInsRedmInfoVO searchAssetsRedmApplyInfo(PrjInsRedmInfoQC qc) throws BaseException {
        if (null == qc) {
            BaseLogUtils.newLogger("查询资管资产赎回申请信息,请求数据为空!").setKeys("searchAssetsRedmApplyInfo","查询条件PrjInsRedmInfoQC="+JSON.toJSONString(qc),CurrentThreadContext.getCurrentUserName()).info();
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        }
        PrjInsRedmInfoDTO dto = this.prjInsRedmInfoApiService.searchAssetsRedmApplyInfo(qc);
        return PrjInsRedmInfoVOTransform.toVO(dto);
    }
    /**
     * 查询资管资产赎回信息
     *
     * @param qc
     * @return
     */
    @Override
    public PrjInsRedmInfoVO searchAssetsRedmInfo(PrjInsRedmInfoQC qc) throws BaseException {
        if (null == qc) {
            BaseLogUtils.newLogger("查询资管资产赎回信息,请求数据为空!").setKeys("searchAssetsRedmInfo","查询条件PrjInsRedmInfoQC="+JSON.toJSONString(qc),CurrentThreadContext.getCurrentUserName()).info();
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        }
        PrjInsRedmInfoDTO prjInsRedmInfoDTO = this.prjInsRedmInfoApiService.searchAssetsRedmInfo(qc);
        BaseLogUtils.newLogger("查询资管赎回信息结束!").setKeys("searchAssetsRedmInfo","PrjInsRedmInfoDTO="+JSON.toJSONString(prjInsRedmInfoDTO)).info();
        return PrjInsRedmInfoVOTransform.toVO(prjInsRedmInfoDTO);
    }

    /**
     * 发起流程
     * @param prjInsRedmInfoVO
     * @param currentUserId
     * @param currentUserName
     * @param currentBuOrgCd
     * @param currentOrgCd
     * @param sysCd
     * @return
     * @throws BaseException
     */
    @Override
    public FlowStartedDTO addRedmAndFlowStart(PrjInsRedmInfoDTO prjInsRedmInfoDTO, Long currentUserId, String currentUserName, String currentBuOrgCd, String currentOrgCd, String sysCd,Integer isCstMgr) throws BaseException {
        if (null == prjInsRedmInfoDTO || null == currentUserId || StringUtils.isEmpty(currentUserName) || StringUtils.isEmpty(currentBuOrgCd) || StringUtils.isEmpty(currentOrgCd) || StringUtils.isEmpty(sysCd)) {
            BaseLogUtils.newLogger("发起流程,参数不完整!").setKeys("addRedmAndFlowStart","prjInsRedmInfoDTO:" + JSON.toJSONString(prjInsRedmInfoDTO) + ",currentUserId:" + currentUserId
                    + ",currentUserName:" + currentUserName + ",currentBuOrgCd:" + currentBuOrgCd + ",currentOrgCd:" + currentOrgCd + ",sysCd:" + sysCd,CurrentThreadContext.getCurrentUserName()).info();
            throw new BaseException("发起流程,参数不完整.");
        }
        BaseLogUtils.newLogger("资管资产赎回,发起流程").setKeys("add","prjInsRedmInfoDTO="+ JSON.toJSONString(prjInsRedmInfoDTO)).info();
        return this.prjInsRedmInfoApiService.addRedmAndFlowStart(prjInsRedmInfoDTO,currentUserId,currentUserName,currentBuOrgCd,currentOrgCd,sysCd,isCstMgr);
    }

    /**
     *  查询项目产品信息
     * @param qc
     * @return
     * @throws BaseException
     */
    @Override
    public List<PrjDtlInsRedmInfoVO> selectPrjDtlInfo(PrjInsRedmInfoQC qc) throws BaseException{
        if (null == qc || StringUtils.isEmpty(qc.getPrjId())) {
            BaseLogUtils.newLogger("查询项目产品信息失败,参数不完整!").setKeys("selectPrjDtlInfo","查询条件PrjInsRedmInfoQC="+JSON.toJSONString(qc),CurrentThreadContext.getCurrentUserName()).info();
            throw new BaseException("查询项目产品信息失败,参数不完整.");
        }
        return PrjInsRedmInfoVOTransform.toPrjDtlVOList(this.prjInsRedmInfoApiService.selectPrjDtlInfo(qc));
    }


    @Override
    public DataResultDTO addPrjInsRedmInfoAndVerifyFlow(PrjInsRedmInfoVO entity) throws BaseException {
        if (null == entity) {
            BaseLogUtils.newLogger("新增对象不存在!").setKeys("addPrjInsRedmInfoAndVerifyFlow",CurrentThreadContext.getCurrentUserName()).info();
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "新增对象不存在!");
        }
        return this.prjInsRedmInfoApiService.addPrjInsRedmInfoAndVerifyFlow(PrjInsRedmInfoVOTransform.toDTO(entity));
    }

    @Override
    public List<CapFundVO> searchVfInfo4InsRedeem(Long paySchId) throws BaseException {
        return CapFundVOTransform.toVOList(this.prjInsRedmInfoApiService.searchVfInfo4InsRedeem(paySchId));
    }

    /**
     * 客户经理发起赎回流程
     * @param entity
     * @return
     */
    @Override
    public DataResultDTO addPrjInsRedmInfoByCustomerManager(PrjInsRedmInfoVO entity) throws BaseException {
        if (null == entity) {
            BaseLogUtils.newLogger("新增对象不存在!").setKeys("addPrjInsRedmInfoByCustomerManager",CurrentThreadContext.getCurrentUserName()).info();
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "新增对象不存在!");
        }
        return this.prjInsRedmInfoApiService.addPrjInsRedmInfoByCustomerManager(PrjInsRedmInfoVOTransform.toDTO(entity));
    }

    /**
     * 试算数据校验
     */
    @Override
    public DataResultDTO verifyInsRedmTrialData(PrjInsRedmInfoVO entity) throws BaseException {
        return this.prjInsRedmInfoApiService.verifyInsRedmTrialData(PrjInsRedmInfoVOTransform.toDTO(entity));
    }

    /**
     * 添加试算数据 添加赎回记录
     * @param prjInsRedmInfoDTO
     * @param prjInsRedmInterfaceInfoDTO
     * @return
     * @throws BaseException
     */
    @Override
    public DataResultDTO addPrjInsRedmData(PrjInsRedmInfoDTO prjInsRedmInfoDTO, PrjInsRedmInterfaceInfoDTO prjInsRedmInterfaceInfoDTO) throws BaseException {
        DataResultDTO dataResultDTO = new DataResultDTO(BaseConstant.IS_YESNO_YES,"添加数据成功");
        if (null == prjInsRedmInfoDTO) {
            BaseLogUtils.newLogger("新增对象不存在!").setKeys("addPrjInsRedmData",CurrentThreadContext.getCurrentUserName()).info();
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "新增对象不存在!");
        }
        BaseLogUtils.newLogger("开始新增赎回记录!").setKeys("addPrjInsRedmData","prjInsRedmInfoDTO="+ JSON.toJSONString(prjInsRedmInfoDTO),CurrentThreadContext.getCurrentUserName()).info();
        if(null != prjInsRedmInterfaceInfoDTO){
            /**
             * 试算成功设置赎回记录中赎回本金 赎回利息 赎回金额
             */
            prjInsRedmInfoDTO.setRedmTotalAmt(prjInsRedmInterfaceInfoDTO.getRedmAmt() == null?0D:prjInsRedmInterfaceInfoDTO.getRedmAmt());
            prjInsRedmInfoDTO.setRedmPrinAmt(prjInsRedmInterfaceInfoDTO.getPrincipalAmt() == null?0D:prjInsRedmInterfaceInfoDTO.getPrincipalAmt());
            prjInsRedmInfoDTO.setRedmIntAmt(prjInsRedmInterfaceInfoDTO.getInterestAmt() == null?0D:prjInsRedmInterfaceInfoDTO.getInterestAmt());
        }
        Long redmId = this.prjInsRedmInfoApiService.add(prjInsRedmInfoDTO);
        /**
         * 资方已结清发起流程无试算记录
         */
        if(null != redmId){
            if(null != prjInsRedmInterfaceInfoDTO){
                BaseLogUtils.newLogger("开始新增试算记录!").setKeys("addPrjInsRedmData","prjInsRedmInterfaceInfoDTO="+ JSON.toJSONString(prjInsRedmInterfaceInfoDTO),CurrentThreadContext.getCurrentUserName()).info();
                Long id = this.prjInsRedmInterfaceInfoApiService.add(prjInsRedmInterfaceInfoDTO);
                if(null != id){
                    dataResultDTO.setData(redmId);
                }else{
                    throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "新增试算记录失败!");
                }
            }else{
                dataResultDTO.setData(redmId);
            }
        }else{
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "新增赎回记录失败!");
        }

        BaseLogUtils.newLogger("新增赎回试算记录结束!").setKeys("addPrjInsRedmData","dataResultDTO="+ JSON.toJSONString(dataResultDTO),CurrentThreadContext.getCurrentUserName()).info();
        return dataResultDTO;
    }

    /**
     * 修改赎回记录 试算记录
     * @param entity
     * @return
     */
    @Override
    public DataResultDTO modifyPrjInsRedmData(PrjInsRedmInfoDTO prjInsRedmInfoDTO, PrjInsRedmInterfaceInfoDTO prjInsRedmInterfaceInfoDTO) throws BaseException {
        DataResultDTO dataResultDTO = new DataResultDTO(BaseConstant.IS_YESNO_YES,"修改数据成功");
        if (null == prjInsRedmInfoDTO) {
            BaseLogUtils.newLogger("修改对象不存在!").setKeys("modifyPrjInsRedmData",CurrentThreadContext.getCurrentUserName()).info();
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "修改对象不存在!");
        }
        boolean flag = this.prjInsRedmInfoApiService.modify(prjInsRedmInfoDTO);
        if(flag){
            /**
             * 查询试算记录
             */
            PrjInsRedmInterfaceInfoQC prjInsRedmInterfaceInfoQC = new PrjInsRedmInterfaceInfoQC();
            prjInsRedmInterfaceInfoQC.setPrjId(prjInsRedmInfoDTO.getPrjId());
            prjInsRedmInterfaceInfoQC.setPaySchId(prjInsRedmInfoDTO.getPaySchId());
            prjInsRedmInterfaceInfoQC.setInsCode(prjInsRedmInfoDTO.getInsCode());
            prjInsRedmInterfaceInfoQC.setPaySchNo(prjInsRedmInfoDTO.getPaySchNo());
            prjInsRedmInterfaceInfoQC.setOperationType(PrjInsRedmInterfaceInfoConstant.INS_REDM_INTERFACE_OPERATION_TYPE_0);
            BaseLogUtils.newLogger("查询资方试算记录开始").setKeys("modifyPrjInsRedmData","prjInsRedmInterfaceInfoQC="+ JSON.toJSONString(prjInsRedmInterfaceInfoQC)).info();
            List<PrjInsRedmInterfaceInfoDTO> prjInsRedmInfoDTOList = prjInsRedmInterfaceInfoApiService.selectInsRedmInterfaceInfoList(prjInsRedmInterfaceInfoQC);
            BaseLogUtils.newLogger("查询资方试算记录结束").setKeys("modifyPrjInsRedmData","prjInsRedmInfoDTOListSize="+ prjInsRedmInfoDTOList.size()).info();

             if(CollectionUtils.isNotEmpty(prjInsRedmInfoDTOList)){
                /**
                 * 存在试算记录 更新
                 */
                boolean result = this.prjInsRedmInterfaceInfoApiService.modify(prjInsRedmInterfaceInfoDTO);
                if(!result){
                    throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "修改试算记录失败!");
                }
            }else{
                /**
                 * 不存在试算记录 新增
                 */
                Long id = this.prjInsRedmInterfaceInfoApiService.add(prjInsRedmInterfaceInfoDTO);
                if(null == id){
                    throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "新增试算记录失败!");
                }
            }
        }else{
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "修改赎回记录失败!");

        }
        return dataResultDTO;
    }
}
